﻿--[[
   @brief    client global tips show frame
   @author   jadonsun
   @version  1.0
   @date     2017-04-18
--]]

client_tips_model_constant =
{
    TIPS_MAIN_TYPE_NORMAL = 0;
    TIPS_MAIN_TYPE_BUBBLE = 1;
    TIPS_MAIN_TYPE_WEB = 2;
    TOTAL_BUBBLE_TIPS_NUM = 3;

    TIPS_BUBBLE_TIMEOUT = 5000;

}
client_tips_frame_model =
{

    -- 存储所有进入tips管理器，但是并没有被处理的tips
    all_tips_queue = {};

    -- 存储显示的tips内容队列
    bubble_tips_queue = {};
    normal_tips_queue = {};
    web_tips_queue = {};

    -- 存储显示的tips dlg队列
    bubble_dlg_queue = {};
    normal_dlg_queue = {};
    web_dl_queue = {};

    -- 存储所有将要被删除，但是并还没有被删除的tips
    wait_del_tips_queue = {};

    -- 应用(游戏)退出后的操作配置
    apps_exit_tips_config = {};
    dlg_to_app_exit_config = {};
    kAppExitConfigURL = LoadStr("GL_GAME_CONFIG_URL");
    -- oss请求相关数据
    tips_oss_js = "global_tips_info.js";
    tips_id_list = {};

    -- 标记游戏是否在运行中
    is_game_running = false;

    init = function ()
        base_utility.log("[client_tips_frame_model] init");

        -- 加载oss配置
        ierd_tgp.tpf_ui.Require("common\\OssConfig.lua");
        TOssConfig.InitConfig();

        client_tips_frame_model.req_oss_config();
        client_tips_frame_model.request_app_exit_config(0);
    end;

    req_oss_config = function ()
        -- 请求tips id信息
        local z_param = {};
        z_param["file" ] = client_tips_frame_model.tips_oss_js;
        z_param["sink" ] = "client_tips_frame_model.on_resp_oss_config";
        z_param["cache"] = true;

        comm_center.call_service("Svr_GetOssCfg", z_param);
    end;

    -- 请求oss返回结果
    on_resp_oss_config = function(s_file, z_json)
        if (nil == z_json) or (nil == s_file)
                or (client_tips_frame_model.tips_oss_js ~= s_file) then
            base_utility.log("[client_tips_frame_model] get tips id oss config failed");
            return;
        end;

        local z_data = z_json["data"];
        if (nil == z_data) then
            base_utility.log("[client_tips_frame_model] get tips id oss config failed");
            return;
        end;

        base_utility.log("[client_tips_frame_model] get tips type oss successed");

        -- 解析oss数据，得到已注册的tips_id
        for index = 1, #(z_data) do
            local z_temp = z_data[index];
            client_tips_frame_model.tips_id_list[index] = tonumber(z_temp["tips_type_id"]);
        end;

    end;

    -- 获取游戏运行状态
    get_game_runing_state = function (event_id, param)
        local tb_game_state = base_utility.json_to_table(param);
        if tb_game_state ~= nil and tb_game_state.running_state ~= nil then
            if tb_game_state.running_state > 0 then
                client_tips_frame_model.is_game_running = true;
            else
                client_tips_frame_model.is_game_running = false;
            end;
            base_utility.log("[client_tips_frame_model] get game state is " .. tb_game_state.running_state);
        end;
    end;

    get_game_end_state = function (event_id, param)
        -- 这里监听下游戏结束事件，置状态
        client_tips_frame_model.is_game_running = false;
        if nil ~= param then
            local param_tbl = base_utility.json_to_table(param);
            if nil ~= param_tbl.game_id then
                local cfg = client_tips_frame_model.get_app_exit_config(param_tbl.game_id);
                if nil ~= cfg then
                    client_tips_frame_view.show_bkd_tips(cfg);
                end
            end
        end
        
    end;

    -- 获得当前的窗口类型:normal or bubble or web
    get_dlg_type = function(dlg_id)
        local tip_id = 0;
        for key, value in pairs(client_tips_frame_model.normal_dlg_queue) do
            if value == dlg_id then
                tip_id = key;
                break;
            end;
        end;
        if tip_id > 0 then
            return client_tips_model_constant.TIPS_MAIN_TYPE_NORMAL;
        end;
        for key, value in pairs(client_tips_frame_model.bubble_dlg_queue) do
            if value == dlg_id then
                tip_id = key;
                break;
            end;
        end;
        if tip_id > 0 then
            return client_tips_model_constant.TIPS_MAIN_TYPE_BUBBLE;
        end;
        return client_tips_model_constant.TIPS_MAIN_TYPE_WEB;
    end;

    -- 根据tip id 获取normal tips的dlg id
    get_normal_tip_dlg_id = function (tip_id)
        local tip_dlg_id = 0;
        for key, value in pairs(client_tips_frame_model.normal_dlg_queue) do
            if key == tip_id then
                tip_dlg_id = value;
                break;
            end;
        end;
        return tip_dlg_id;
    end;

    -- 根据 dlg id 获得 normal tip的tip id
    get_normal_tip_id = function (dlg_id)
        local tip_id = 0;
        for key, value in pairs(client_tips_frame_model.normal_dlg_queue) do
            if value == dlg_id then
                tip_id = key;
                break;
            end;
        end;
        return tip_id;
    end;

    -- 根据tip id 获取bubble tips的dlg id
    get_bubble_tip_dlg_id = function (tip_id)
        local tip_dlg_id = 0;
        for key, value in pairs(client_tips_frame_model.bubble_dlg_queue) do
            if key == tip_id then
                tip_dlg_id = value;
                break;
            end;
        end;
        return tip_dlg_id;
    end;

    -- 根据 dlg id 获得 bubble tip的tip id
    get_bubble_tip_id = function (dlg_id)
        local tip_id = 0;
        for key, value in pairs(client_tips_frame_model.bubble_dlg_queue) do
            if value == dlg_id then
                tip_id = key;
                break;
            end;
        end;
        return tip_id;
    end;

    -- 根据tip id 获得 normal tip data
    get_normal_tip_by_id = function (tip_id)
        local tip_num = #(client_tips_frame_model.normal_tips_queue);
        local tip_data = nil;
        for index = 1, tip_num do
            if client_tips_frame_model.normal_tips_queue[index].id == tip_id then
                tip_data = client_tips_frame_model.normal_tips_queue[index];
                break;
            end;
        end;
        return tip_data;
    end;

    -- 根据tip id 获得 bubble tip data
    get_bubble_tip_by_id = function (tip_id)
        local tip_num = #(client_tips_frame_model.bubble_tips_queue);
        local tip_data = nil;
        for index = 1, tip_num do
            if client_tips_frame_model.bubble_tips_queue[index].id == tip_id then
                tip_data = client_tips_frame_model.bubble_tips_queue[index];
                break;
            end;
        end;
        return tip_data;
    end;

    request_app_exit_config = function(index)
        if nil == index then
            index = 0;
        end
    
        local client_type = 1;
        if base_utility.is_type_of_oversea_ver() then
            client_type = 2;
        end

        local post_data = {
                data_names = "app_exit_tips_config",
                command = "list_all",
                params = {
                    start_page = index,
                    items_per_pager = 50
                },
                stamp = {
                    credential = {
                        agent_client_type = client_type,
                    },
                    agent_client_language = base_utility.get_currrent_language_str();
                }
            };
        base_utility.curl_request_async(client_tips_frame_model.kAppExitConfigURL
            , "client_tips_frame_model.on_request_app_exit_config"
            , false, nil, true, nil, base_utility.table_to_json(post_data));
    end;

    on_request_app_exit_config = function(response)
        base_utility.log("[client_tips_frame_model]app exit tips config ".. response);
        local res_tbl = base_utility.json_to_table(response);
        if nil == res_tbl or nil == res_tbl.result then
            return;
        end
        if 0 ~= tonumber(res_tbl.result.error_code) then
            return;
        end

        local items_count = 0;
        if nil ~= res_tbl.items and type(res_tbl.items) == "table" then
            items_count = #res_tbl.items;
            for k, v in pairs(res_tbl.items) do
                table.insert(client_tips_frame_model.apps_exit_tips_config, v);
            end
        end

        -- request more
        local total_items = tonumber(res_tbl.total_items or "0");
        local start_page = tonumber(res_tbl.start_page or "0");
        local items_per_page = tonumber(res_tbl.items_per_pager or "0");

        if total_items > (start_page * items_per_page + items_count) then
            client_tips_frame_model.request_app_exit_config(start_page + 1);
        end
    end;

    get_app_exit_config = function(game_id)
        for k, v in pairs(client_tips_frame_model.apps_exit_tips_config) do
            if tonumber(v["game_id"]) == game_id then
                return v;
            end
        end
        return nil;
    end;

    add_dlg_to_app_exit_config = function(dlg_id, cfg)
        client_tips_frame_model.dlg_to_app_exit_config[dlg_id] = cfg;
    end;

    get_app_exit_config_by_dlg = function(dlg_id)
        return client_tips_frame_model.dlg_to_app_exit_config[dlg_id];
    end;

    is_app_exit_tips_showing = function(game_id)
        for k, v in pairs(client_tips_frame_model.dlg_to_app_exit_config) do
            if tonumber(v["game_id"]) == tonumber(game_id) then
                return true;
            end
        end
        return false;
    end;

    get_app_exit_local_config = function(game_id)
        local tgp_id = base_utility.get_tgp_id();
        local data = {};
        data["common"] = 0;
        data["key"] = "client_tips.exit_app."..tostring(tgp_id).."."..tostring(game_id);
        local config = comm_center.call_service("Svr_GetConfig", data);
        return config;
    end;

    set_app_exit_local_config = function(game_id, time)
        local last_show_time = os.time();
        if nil ~= time then
            last_show_time = time;
        end
        local tgp_id = base_utility.get_tgp_id();
        local data = {};
        data["common"] = 0;
        data["key"] = "client_tips.exit_app."..tostring(tgp_id).."."..tostring(game_id);
        local config_tbl = {};
        config_tbl["last_show_time"] = last_show_time;
        data["value"] = base_utility.table_to_json(config_tbl);
        comm_center.call_service("Svr_SetConfig", data);
    end;

}